package dmt16.TelemetrySuit.UI
{
	import dmt16.TelemetrySuit.UI.Assets.Fonts;
	import dmt16.TelemetrySuit.UI.Tiles.TileBase;
	import dmt16.TelemetrySuit.Utils.StringUtils;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	public class StopWatch extends Sprite
	{
		public function StopWatch()
		{
			addEventListener(Event.ENTER_FRAME,_onEnterFrame);
			_display=new TextField();
			_display.embedFonts=true;
			_display.defaultTextFormat=_textFormat;
			_display.selectable=false;
			_display.text="00:00:00.000";
			_display.x=20;
			
			addChild(_display);
			
			_startStopBtn=new Sprite();
			_startStopBtn.graphics.lineStyle(1,TileBase.FG_COLOUR);
			_startStopBtn.graphics.drawRect(0,0,100,40);
			_startStopTxt=new TextField();
			_startStopTxt.embedFonts=true;
			_startStopTxt.selectable=false;
			_startStopTxt.defaultTextFormat=_textFormatBtn;
			_startStopTxt.text="start";
			_startStopTxt.width=100;
			_startStopTxt.height=30;
			_startStopTxt.y=5;
			_startStopBtn.addChild(_startStopTxt);
			_startStopBtn.addEventListener(MouseEvent.CLICK,_onStartStopClick);
						
			_startStopBtn.x=57;
			_startStopBtn.y=60;
			
			addChild(_startStopBtn);
			
			_lapResetBtn=new Sprite();
			_lapResetBtn.graphics.lineStyle(1,TileBase.FG_COLOUR);
			_lapResetBtn.graphics.drawRect(0,0,100,40);
			_lapResetTxt=new TextField();
			_lapResetTxt.embedFonts=true;
			_lapResetTxt.selectable=false;
			_lapResetTxt.defaultTextFormat=_textFormatBtn;
			_lapResetTxt.text="reset";
			_lapResetTxt.width=100;
			_lapResetTxt.height=30;
			_lapResetTxt.y=5;
			_lapResetBtn.addChild(_lapResetTxt);
			_lapResetBtn.addEventListener(MouseEvent.CLICK,_onLapResetClick);
			
			_lapResetBtn.x=194;
			_lapResetBtn.y=60;
			
			addChild(_lapResetBtn);
			
		}
		
		private function _onStartStopClick(event:MouseEvent):void{
			if(_startStopTxt.text=="start")
				_start();
			else
				_stop();
		}
		
		private function _onLapResetClick(event:MouseEvent):void{
			if(_lapResetTxt.text=="reset")
				_reset();
			else
				_lap();
		}
		
		private var _startStopBtn:Sprite;
		private var _lapResetBtn:Sprite;
		
		private var _display:TextField;
		private var _startStopTxt:TextField;
		private var _lapResetTxt:TextField;
		
		private static var _textFormat:TextFormat;
		{
			_textFormat=new TextFormat();
			_textFormat.font=Fonts.DSDigitalBold;
			_textFormat.size=60;
			_textFormat.color=TileBase.FG_COLOUR;
		}
		
		private static var _textFormatBtn:TextFormat;
		{
			_textFormatBtn=new TextFormat();
			_textFormatBtn.font=Fonts.DSDigital;
			_textFormatBtn.size=25;
			_textFormatBtn.color=TileBase.FG_COLOUR;
			_textFormatBtn.align=TextFormatAlign.CENTER;
		}
		
		private function _start():void{
			if(_paused&&!_rst){
				_running=true;
				_paused=false;
				_rst=false;
				_startTime=new Date().time-_pauseTime;
			}
			else if(_rst){
				_running=true;
				_paused=false;
				_rst=false;
				_startTime=new Date().time;
			}
			else{
				_running=true;
				_paused=false;
				_rst=false;
			}
			_startStopTxt.text="stop";
			_lapResetTxt.text="lap";
		}
		
		private function _stop():void{
			_running=false;
			_paused=true;
			_rst=false;
			_pauseTime=new Date().time-_startTime;
			var now:Date=new Date();
			now.time=now.time-_startTime;
			_display.text=StringUtils.lpad(now.hours.toString(),2)+":"+
				StringUtils.lpad(now.minutes.toString(),2)+":"+
				StringUtils.lpad(now.seconds.toString(),2)+"."+
				StringUtils.lpad(now.milliseconds.toString(),3);
			_startStopTxt.text="start";
			_lapResetTxt.text="reset";
		}
		
		private function _lap():void{
			_running=false;
			_paused=false;
			_rst=false;
			var now:Date=new Date();
			now.time=now.time-_startTime;
			_display.text=StringUtils.lpad(now.hours.toString(),2)+":"+
				StringUtils.lpad(now.minutes.toString(),2)+":"+
				StringUtils.lpad(now.seconds.toString(),2)+"."+
				StringUtils.lpad(now.milliseconds.toString(),3);
			_startStopTxt.text="start";
			_lapResetTxt.text="lap";
		}
		
		private function _reset():void{
			_rst=true;
			_display.text="00:00:00.000";
		}
		
		private var _width:Number=100;
		public override function get width():Number{
			return _width;
		}
		public override function set width(value:Number):void{
			_width=value;
		}
		
		private var _height:Number=250;
		public override function get height():Number{
			return _height;
		}
		public override function set height(value:Number):void{
			_height=value;
		}
		
		private var _running:Boolean=false;
		private var _paused:Boolean=false;
		private var _pauseTime:Number;
		private var _rst:Boolean=true;
		
		private var _startTime:Number=new Date().time;
						
		private function _onEnterFrame(event:Event):void{
			if(_running){
				var now:Date=new Date();
				now.time=now.time-_startTime;
				_display.text=StringUtils.lpad(now.hours.toString(),2)+":"+
					StringUtils.lpad(now.minutes.toString(),2)+":"+
					StringUtils.lpad(now.seconds.toString(),2)+"."+
					StringUtils.lpad(now.milliseconds.toString(),3);
			}
			_display.width=_width-40;
			_display.height=_height;
		}
	}
}